package com.neusoft.SPNursingHome.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.SPNursingHome.po.Client;
import com.neusoft.SPNursingHome.po.ClientMealInfo;
import com.neusoft.SPNursingHome.po.MealCalendarWithFood;
import com.neusoft.SPNursingHome.po.MealRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface MealRecordDao extends BaseMapper<MealRecord> {

    /**
     * 分页查询某个 clientId 的订餐记录
     * @param page     分页对象（包含 page、size）
     * @param clientId 客户ID
     */
    @Select("""
        <script>
        SELECT
          mr.meal_record_id      AS mealRecordId,
          mc.meal_calendar_id    AS mealCalendarId,
          mr.count      AS count,
          mr.client_id           AS clientId,
          c.client_name          AS clientName,
          mc.meal_calendar_date  AS mealCalendarDate,
          mc.meal_calendar_time  AS mealCalendarTime,
          mr.meal_calendar_day   AS mealCalendarDay,
          f.food_type            AS foodType,
          f.food_name            AS foodName
        FROM meal_record mr
        JOIN meal_calendar mc ON mr.meal_calendar_id = mc.meal_calendar_id
        JOIN client c        ON mr.client_id          = c.client_id
        JOIN food f          ON mc.food_id            = f.food_id
        WHERE mr.client_id = #{clientId}
        
        <!-- 可选：起始日期 -->
        <if test="startDay != null and startDay.trim() != ''">
          AND mr.meal_calendar_day &gt;= #{startDay}
        </if>
        <!-- 可选：结束日期 -->
        <if test="endDay   != null and endDay.trim()   != ''">
          AND mr.meal_calendar_day &lt;= #{endDay}
        </if>
        
        <!-- 其余过滤 -->
        <if test="foodType != null and foodType.trim() != ''">
          AND f.food_type LIKE CONCAT('%', #{foodType}, '%')
        </if>
        <if test="foodName != null and foodName.trim() != ''">
          AND f.food_name LIKE CONCAT('%', #{foodName}, '%')
        </if>
        <if test="mealCalendarTime != null and mealCalendarTime.trim() != ''">
          AND mc.meal_calendar_time LIKE CONCAT('%', #{mealCalendarTime}, '%')
        </if>
        
        ORDER BY mr.meal_calendar_day DESC
        </script>
    """)
    IPage<ClientMealInfo> selectByClientAndFilter(
            Page<?> page,
            @Param("clientId") Integer clientId,
            @Param("startDay") String startDay,
            @Param("endDay") String endDay,
            @Param("foodType") String foodType,
            @Param("foodName") String foodName,
            @Param("mealCalendarTime") String mealCalendarTime
    );

    @Select({
            "<script>",
            "SELECT",
            "  c.client_name        AS clientName,",
            "  f.food_type          AS foodType,",
            "  f.food_name          AS foodName,",
            "  mc.meal_calendar_date AS mealCalendarDate,",
            "  mr.meal_calendar_day  AS mealCalendarDay",
            "FROM meal_record mr",
            "  JOIN client c       ON mr.client_id       = c.client_id",
            "  JOIN meal_calendar mc ON mr.meal_calendar_id = mc.meal_calendar_id",
            "  JOIN food f         ON mc.food_id         = f.food_id",
            "<where>",
            "  <if test='name != null and name.trim().length() > 0'>",
            "    AND c.client_name LIKE CONCAT('%', #{name}, '%')",
            "  </if>",
            "</where>",
            "</script>"
    })
    IPage<ClientMealInfo> selectClientMealInfoByName(IPage<ClientMealInfo> page,String name);
    @Select("""
  SELECT meal_record_id, client_id, meal_calendar_id, meal_calendar_day, count
  FROM meal_record
  WHERE client_id = #{clientId}
    AND meal_calendar_id = #{mealCalendarId}
    AND meal_calendar_day = #{mealCalendarDay}
  LIMIT 1
""")
    MealRecord selectByUniqueKeys(
            @Param("clientId") Integer clientId,
            @Param("mealCalendarId") Integer mealCalendarId,
            @Param("mealCalendarDay") LocalDate mealCalendarDay
    );
}
